<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
<meta name="baidu-site-verification" content="code-hkfwkpUecU" />
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="true">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto+Slab:300,300italic,400,400italic,700,700italic|PT+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"github.com","root":"/","scheme":"Gemini","version":"8.0.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>

  <meta property="og:type" content="website">
<meta property="og:title" content="shanlu的博客">
<meta property="og:url" content="https://github.com/shanlu123/page/10/index.html">
<meta property="og:site_name" content="shanlu的博客">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="shanlu">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://github.com/shanlu123/page/10/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>

  <title>shanlu的博客</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>
  <a href="https://github.com/shanlu123" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">shanlu的博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">三餐四季，冷暖自知</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/home" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-前端">

    <a href="/%E5%89%8D%E7%AB%AF/" rel="section"><i class="fa fa-cloud fa-fw"></i>前端</a>

  </li>
        <li class="menu-item menu-item-生活">

    <a href="/%E7%94%9F%E6%B4%BB/" rel="section"><i class="fa fa-user fa-fw"></i>生活</a>

  </li>
        <li class="menu-item menu-item-school">

    <a href="/school/" rel="section"><i class="fa fa-graduation-cap fa-fw"></i>学校</a>

  </li>
        <li class="menu-item menu-item-tree_hole">

    <a href="/tree_hole/" rel="section"><i class="fa fa-tree fa-fw"></i>树洞</a>

  </li>
        <li class="menu-item menu-item-photos">

    <a href="/photos/" rel="section"><i class="fa fa-camera fa-fw"></i>相册</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="shanlu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">shanlu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/shanlu123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;shanlu123"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.cnblogs.com/shanlu0000/p/11478994.html" title="博客园 → https:&#x2F;&#x2F;www.cnblogs.com&#x2F;shanlu0000&#x2F;p&#x2F;11478994.html" rel="noopener" target="_blank"><i class="fa fa-blog fa-fw"></i>博客园</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:yourname@gmail.com" title="E-Mail → mailto:yourname@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/6662419816/home?wvr=5" title="微博 → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;6662419816&#x2F;home?wvr&#x3D;5" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>微博</a>
      </span>
  </div>


  <div class="links-of-blogroll animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.npmjs.com/" title="https:&#x2F;&#x2F;www.npmjs.com&#x2F;" rel="noopener" target="_blank">npm</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.bilibili.com/" title="https:&#x2F;&#x2F;www.bilibili.com&#x2F;" rel="noopener" target="_blank">bilibili</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="noopener" target="_blank">stackoverflow</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://developers.weixin.qq.com/miniprogram/dev/component/" title="https:&#x2F;&#x2F;developers.weixin.qq.com&#x2F;miniprogram&#x2F;dev&#x2F;component&#x2F;" rel="noopener" target="_blank">小程序</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://www.ruanyifeng.com/blog/" title="http:&#x2F;&#x2F;www.ruanyifeng.com&#x2F;blog&#x2F;" rel="noopener" target="_blank">阮一峰</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://cn.aliyun.com/" title="https:&#x2F;&#x2F;cn.aliyun.com&#x2F;" rel="noopener" target="_blank">阿里云</a>
        </li>
    </ul>
  </div>

      </section>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">
      

      
      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/10/01/python/python%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/10/01/python/python%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" class="post-title-link" itemprop="url">python数据类型</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-10-01 00:00:00" itemprop="dateCreated datePublished" datetime="2020-10-01T00:00:00+08:00">2020-10-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:08:42" itemprop="dateModified" datetime="2020-10-04T17:08:42+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/10/01/python/python%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" class="post-meta-item leancloud_visitors" data-flag-title="python数据类型" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/10/01/python/python%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/10/01/python/python%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.3k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>5 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h3><ul>
<li>python中的数据类型指的是变量的值的类型</li>
</ul>
<h3 id="数值型"><a href="#数值型" class="headerlink" title="数值型"></a>数值型</h3><ol>
<li><p>分为三种：整数，浮点数，复数</p>
</li>
<li><p>python中所有的整数都是int类型， python中所有的小数都是 flota类型</p>
</li>
<li><p>python中的整数的大小没有限制，可以是一个无限大的整数</p>
</li>
<li><p>对浮点数进行运算，可能得到一个不精确结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">c = <span class="number">0.1</span> + <span class="number">0.2</span>  <span class="comment"># 0.30000000000000004</span></span><br><span class="line">print(c)</span><br></pre></td></tr></table></figure>
</li>
<li><p>如果数字的长度过大，可以使用下划线作为分隔符 </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">c = <span class="number">123</span>_456_789</span><br><span class="line">print(c)  <span class="comment"># 123456789</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>注意，10进制数字不能以0开头，如，不能写0123，其他进制的整数，打印数字时以十进制显示</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 二进制 0b开头</span></span><br><span class="line">a = <span class="number">0b10</span>  <span class="comment"># 二进制的10</span></span><br><span class="line"><span class="comment"># 八进制 0o开头</span></span><br><span class="line">b = <span class="number">0o10</span></span><br><span class="line"><span class="comment"># 十六进制 0x开头</span></span><br><span class="line">c = <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line">print(<span class="string">&quot;a=&quot;</span>, a, <span class="string">&quot;b=&quot;</span>, b, <span class="string">&quot;c=&quot;</span>, c)  <span class="comment"># a= 2 b= 8 c= 16</span></span><br></pre></td></tr></table></figure>

</li>
</ol>
<h3 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h3>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/10/01/python/python%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/" class="post-title-link" itemprop="url">标准库</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-30 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-30T00:00:00+08:00">2020-09-30</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:05:21" itemprop="dateModified" datetime="2020-10-04T17:05:21+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/" class="post-meta-item leancloud_visitors" data-flag-title="标准库" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>7.6k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>7 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="操作系统接口"><a href="#操作系统接口" class="headerlink" title="操作系统接口"></a>操作系统接口</h3><ul>
<li><p>os 模块提供了很多与操作系统交互的函数</p>
<figure class="highlight moonscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="built_in">os</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">os</span>.getcwd()) # 当前目录路径</span><br></pre></td></tr></table></figure>

<p>只能使用 import os 而不是 from os  import *，这将避免内建的 open() 函数被 os.open() 隐式替换掉</p>
</li>
<li><p>内置的 dir() 和 help() 函数可用作交互式辅助工具，用于处理大型模块，如 os</p>
<figure class="highlight python-repl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python"><span class="keyword">import</span> os</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python">dir(os)  <span class="comment"># 模块列表</span></span></span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python">help(os) </span></span><br></pre></td></tr></table></figure>
</li>
<li><p>对于日常文件和目录管理任务，shutil 模块提供了更易于使用的更高级别的接口</p>
</li>
</ul>
<h3 id="文件通配符"><a href="#文件通配符" class="headerlink" title="文件通配符"></a>文件通配符</h3><ul>
<li><p>glob 模块提供了一个在目录中使用通配符搜索创建文件列表的函数</p>
<figure class="highlight python-repl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python"><span class="keyword">import</span> glob</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt;</span> <span class="python">glob.glob(<span class="string">&#x27;*.py&#x27;</span>)   <span class="comment"># 找到所有py文件列表</span></span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="命令行参数"><a href="#命令行参数" class="headerlink" title="命令行参数"></a>命令行参数</h3>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/30/%E6%95%B0%E6%8D%AE%E5%BA%93/%E6%A0%87%E5%87%86%E5%BA%93/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/29/python/%E9%94%99%E8%AF%AF%E5%92%8C%E5%BC%82%E5%B8%B8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/29/python/%E9%94%99%E8%AF%AF%E5%92%8C%E5%BC%82%E5%B8%B8/" class="post-title-link" itemprop="url">错误和异常</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-29 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-29T00:00:00+08:00">2020-09-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:05:50" itemprop="dateModified" datetime="2020-10-04T17:05:50+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/29/python/%E9%94%99%E8%AF%AF%E5%92%8C%E5%BC%82%E5%B8%B8/" class="post-meta-item leancloud_visitors" data-flag-title="错误和异常" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/29/python/%E9%94%99%E8%AF%AF%E5%92%8C%E5%BC%82%E5%B8%B8/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/29/python/%E9%94%99%E8%AF%AF%E5%92%8C%E5%BC%82%E5%B8%B8/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>3 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="语法错误"><a href="#语法错误" class="headerlink" title="语法错误"></a>语法错误</h3><ul>
<li>语法错误又称解析错误</li>
</ul>
<h3 id="异常"><a href="#异常" class="headerlink" title="异常"></a>异常</h3><ul>
<li>在执行时检测到的错误被称为 异常</li>
<li>异常有不同的类型，ZeroDivisionError，NameError，TypeError等</li>
</ul>
<h3 id="处理异常"><a href="#处理异常" class="headerlink" title="处理异常"></a>处理异常</h3>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/29/python/%E9%94%99%E8%AF%AF%E5%92%8C%E5%BC%82%E5%B8%B8/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/29/python/%E7%B1%BB%20/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/29/python/%E7%B1%BB%20/" class="post-title-link" itemprop="url">类和对象</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-29 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-29T00:00:00+08:00">2020-09-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:05:38" itemprop="dateModified" datetime="2020-10-04T17:05:38+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/29/python/%E7%B1%BB%20/" class="post-meta-item leancloud_visitors" data-flag-title="类和对象" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/29/python/%E7%B1%BB%20/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/29/python/%E7%B1%BB%20/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>8.4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>8 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="Python作用域和命名空间"><a href="#Python作用域和命名空间" class="headerlink" title="Python作用域和命名空间"></a>Python作用域和命名空间</h3><ol>
<li><p>作用域(scope) 指的是变量生效的区域</p>
</li>
<li><p>python中一共有两种作用域</p>
<ul>
<li><p>全局作用域</p>
<ul>
<li>在程序执行时创建，程序执行结束时销毁</li>
<li>所有函数以外的区域都是全局作用域</li>
<li>在全局作用域中定义的变量都属于全局变量，全局变量可以在程序的任意位置被访问</li>
</ul>
</li>
<li><p>函数作用域</p>
<ul>
<li>在函数调用时创建，在调用结束时销毁</li>
<li>函数每调用一次就会产生一个新的函数作用域</li>
<li>在函数作用域中定义的变量，都是局部变量，只能在函数内部被访问</li>
</ul>
</li>
<li><p>使用变量时，会优先在当前作用域中寻找变量，如果没有则继续去上一级作用域中寻找，知道找到全局作用域，依然未找到，则会抛出异常 NameError</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">20</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn1</span>():</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">fn2</span>():</span></span><br><span class="line">        print(<span class="string">f&#x27;fn2中，a=<span class="subst">&#123;a&#125;</span>&#x27;</span>)</span><br><span class="line">    fn2()</span><br><span class="line"></span><br><span class="line">fn1()</span><br><span class="line"></span><br><span class="line"><span class="comment"># fn2中，a=20</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>在函数内为变量赋值时，默认都是为局部变量赋值，如果希望在函数内部修改全局变量，需要使用 global 关键字来声明变量</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn</span>():</span></span><br><span class="line">    a = <span class="number">10</span></span><br><span class="line">    <span class="keyword">global</span> b</span><br><span class="line">    b = <span class="number">10</span>  <span class="comment"># 修改全局变量</span></span><br><span class="line">    print(<span class="string">f&#x27;函数内部，a=<span class="subst">&#123;a&#125;</span>,b=<span class="subst">&#123;b&#125;</span>&#x27;</span>)  <span class="comment"># 函数内部，a=10,b=10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">fn()</span><br><span class="line">print(<span class="string">f&#x27;函数外部，a=<span class="subst">&#123;a&#125;</span>,b=<span class="subst">&#123;b&#125;</span>&#x27;</span>)  <span class="comment"># 函数外部，a=20,b=10</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
<li><p>命名空间(namespace) 是一个从名字到对象的映射，指的是变量存储的位置，每个变量都需要存储到指定的命名空间中</p>
</li>
<li><p>每一个作用域都有它对应的命名空间</p>
<ul>
<li>全局命名空间，用来保存全局变量</li>
<li>函数命名空间，用来保存函数中的局部变量</li>
</ul>
</li>
<li><p>命名空间实际上就是一个字典，是一个专门用来存储变量的字典</p>
</li>
<li><p>locals() 可以用来获取当前作用域的命名空间，返回的是一个字典</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">scope = locals()</span><br><span class="line">print(type(scope))  <span class="comment"># &lt;class &#x27;dict&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line">a = <span class="number">20</span></span><br><span class="line">print(scope[<span class="string">&#x27;a&#x27;</span>])  <span class="comment"># 20</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 向字典中添加key-value就相当于在全局中创建了一个变量（一般不建议这么做）</span></span><br><span class="line">scope[<span class="string">&#x27;c&#x27;</span>] = <span class="number">1000</span>  </span><br></pre></td></tr></table></figure>

</blockquote>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">20</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn</span>():</span></span><br><span class="line">    a = <span class="number">10</span></span><br><span class="line">    scope = locals()</span><br><span class="line">    print(scope[<span class="string">&#x27;a&#x27;</span>])  <span class="comment"># 10</span></span><br><span class="line"></span><br><span class="line">    global_scope = globals()</span><br><span class="line">    print(global_scope[<span class="string">&#x27;a&#x27;</span>])  <span class="comment"># 20, globals() 函数可以用来在任意位置获取全局命名空间</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">fn()</span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>几个命名空间的例子：</p>
<ul>
<li>存放内置函数的集合</li>
<li>模块中的全局名称</li>
<li>函数调用中的局部名称</li>
<li>从某种意义上说，对象的属性集合也是一种命名空间的形式</li>
</ul>
</li>
<li><p>不同命名空间的名称之间没有关系，两个不同的模块都可以定义一个 maximize 函数而不会产生混淆。</p>
</li>
<li><p>属性可以是只读或者是可写的，可写的属性可以对它赋值，可写的属性可以用 del 语句删除。</p>
</li>
<li><p>不同时刻创建的命名空间拥有不同的生存期，包含内置名称的命名空间是在Python解释器启动时创建的，永远不会被删除。模块的全局命名空间在模块定义被读入时创建，通常，模块命名空间也会持续到解释器退出。内置名称实际上也存在于一个模块中，bulitins 模块。</p>
</li>
</ol>
<blockquote>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 作用域和命名空间</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">scope_test</span>():</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">do_local</span>():</span></span><br><span class="line">        str = <span class="string">&quot;local str&quot;</span></span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">do_nonlocal</span>():</span></span><br><span class="line">        <span class="keyword">nonlocal</span> str</span><br><span class="line">        str = <span class="string">&quot;nonlocal str&quot;</span></span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">do_global</span>():</span></span><br><span class="line">        <span class="keyword">global</span> str</span><br><span class="line">        str = <span class="string">&quot;global str&quot;</span></span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">    str = <span class="string">&quot;hello world&quot;</span></span><br><span class="line">    print(do_local()) 	<span class="comment"># local str</span></span><br><span class="line">    print(do_nonlocal()) <span class="comment"># nonlocal str</span></span><br><span class="line">    print(do_global()) <span class="comment"># global str</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">scope_test()</span><br><span class="line">print(<span class="string">&quot;global str:&quot;</span>, str) <span class="comment"># global str: global str</span></span><br></pre></td></tr></table></figure>

</blockquote>
<h3 id="类"><a href="#类" class="headerlink" title="类"></a>类</h3><ol>
<li><p>类</p>
<ul>
<li>Python中的类提供了面向对象编程的所有标准特性</li>
<li>类继承机制允许多个基类</li>
<li>派生类可以覆盖它基类的任何方法</li>
<li>一个方法可以调用基类中相同名称的方法</li>
</ul>
</li>
<li><p>类定义语法</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> 类名(<span class="params">[父类]</span>):</span></span><br><span class="line">   代码块</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义一个MyClass类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyClass</span>():</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">print(MyClass)  <span class="comment"># &lt;class &#x27;__main__.MyClass&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>在类的代码块中，可以定义变量和函数，类中所定义的变量，将会成为该类实例的公共属性，都可以被该类实例访问。</p>
<p>类中定义的函数叫做方法，同样可以被该类实例所访问，类中的方法每次被调用时，解释器都会自动给方法传递第一个参数，就是调用的对象本身。如果是 p1 调的，第一个参数就是p1对象，如果是 p2 调的，第一个参数就是 p2 对象，一般将这个参数命名为 self</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>:</span></span><br><span class="line">    name = <span class="string">&#x27;张三&#x27;</span>  <span class="comment"># 公共属性，所有实例都可以访问</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">say_hello</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(self)  <span class="comment"># &lt;__main__.Person object at 0x00000298029758B0&gt; </span></span><br><span class="line">        print(<span class="string">&#x27;你好！我是%s&#x27;</span> % self.name)  <span class="comment"># 注意在方法中不能直接访问类中的属性</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">p1 = Person()</span><br><span class="line">p2 = Person()</span><br><span class="line"></span><br><span class="line">print(p1.name)  <span class="comment"># 张三</span></span><br><span class="line">p2.say_hello()  <span class="comment"># 你好！我是张三</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>类的基本结构</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> 类名(<span class="params">[父类]</span>)：</span></span><br><span class="line"><span class="class">	公共属性....</span></span><br><span class="line"><span class="class">	</span></span><br><span class="line"><span class="class">	# 对象的初始化方法</span></span><br><span class="line"><span class="class">	<span class="title">def</span> <span class="title">__init__</span>(<span class="params">self,....</span>):</span></span><br><span class="line">		代码块</span><br><span class="line">	</span><br><span class="line">	<span class="comment"># 其他的方法</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">method_1</span>(<span class="params">self,....</span>)：</span></span><br><span class="line"><span class="function">		代码块</span></span><br><span class="line"><span class="function">	</span></span><br><span class="line"><span class="function">	<span class="title">def</span> <span class="title">method_2</span>(<span class="params">self,....</span>)：</span></span><br><span class="line"><span class="function">		代码块</span></span><br><span class="line"><span class="function">	</span></span><br><span class="line"><span class="function">	......</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>类属性和实例属性</p>
<ul>
<li><p>直接在类中定义的属性是类属性，可以通过类或类的实例被访问到</p>
</li>
<li><p>类属性只能通过类对象修改，无法通过实例修改</p>
</li>
<li><p>通过实例对象添加的属性是实例属性，只能通过实例对象来访问和修改，类对象无法访问和修改</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>(<span class="params">object</span>):</span></span><br><span class="line">    count = <span class="number">0</span>  <span class="comment"># 类属性</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        self.name = <span class="string">&#x27;张三&#x27;</span>  <span class="comment"># 实例属性</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">a = A()</span><br><span class="line">A.count = <span class="number">100</span> <span class="comment"># 类属性</span></span><br><span class="line">print(<span class="string">&#x27;A ,&#x27;</span>, A.count) <span class="comment">#  100  类对象访问类属性</span></span><br><span class="line">print(<span class="string">&#x27;a ,&#x27;</span>, a.count) <span class="comment">#  100  实例对象访问类属性</span></span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;a ,&#x27;</span>, a.name) <span class="comment">#  张三 实例对象访问实例属性</span></span><br><span class="line">print(<span class="string">&#x27;A ,&#x27;</span>, A.name)  <span class="comment"># AttributeError: type object &#x27;A&#x27; has no attribute &#x27;name&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>实例方法</p>
<ul>
<li><p>在类中定义，以selft为第一个参数的方法都是实例方法</p>
</li>
<li><p>实例方法在调用时，python会将调用对象作为 self 传入</p>
</li>
<li><p>实例方法可以通过实例和类去调用，当通过实例调用时，会自动将当前调用对象作为 selft  传入；当通过类调用时，不会自动传递self，必须手动传递self</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 类属性</span></span><br><span class="line"><span class="comment"># 实例属性</span></span><br><span class="line"><span class="comment"># 类方法</span></span><br><span class="line"><span class="comment"># 实例方法</span></span><br><span class="line"><span class="comment"># 静态方法</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>(<span class="params">object</span>):</span></span><br><span class="line">    count = <span class="number">0</span>  <span class="comment"># 类属性</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        self.name = <span class="string">&#x27;张三&#x27;</span>  <span class="comment"># 实例属性</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">test_1</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;test_1方法，一个实例方法 &#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">    @classmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">test_2</span>(<span class="params">cls</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;test_2方法，一个类方法 &#x27;</span>, cls)</span><br><span class="line">        print(<span class="string">&#x27;cls.count=&#x27;</span>, cls.count)</span><br><span class="line"></span><br><span class="line"><span class="meta">    @staticmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">test_3</span>():</span></span><br><span class="line">        print(<span class="string">&#x27;test_3方法，一个静态方法&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">a = A()</span><br><span class="line">a.test_1()  <span class="comment"># 等价于 A.test_1(a)</span></span><br><span class="line"><span class="comment"># test_1方法，一个实例方法</span></span><br><span class="line"></span><br><span class="line">A.test_2()  <span class="comment"># 等价于 a.test_2()</span></span><br><span class="line"><span class="comment"># test_2方法，一个类方法  &lt;class &#x27;__main__.A&#x27;&gt;  cls.count= 0</span></span><br><span class="line">A.test_3()  <span class="comment"># test_3方法，一个静态方法</span></span><br><span class="line">a.test_3()  <span class="comment"># test_3方法，一个静态方法</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>类方法</p>
<ul>
<li>在类内部使用@classmethod 来修饰的方法</li>
<li>类方法的第一个参数是 cls，也会被自动传递，cls 就是当前的类对象</li>
</ul>
</li>
<li><p>静态方法</p>
<ul>
<li>在类中使用 @staticmethod 来修饰的方法</li>
<li>静态方法不需要指定任何默认参数，可以通过类和实例去调用</li>
<li>一般都是一些工具方法，和当前类无关</li>
</ul>
</li>
<li><p>类对象</p>
<ul>
<li><p>类对象支持两种操作：属性引用和实例化</p>
</li>
<li><p>属性引用使用 python 中所有属性引用所使用的标准语法：obj.name，当类定义是这样的：</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyClass</span>:</span></span><br><span class="line">	<span class="string">&quot;A simple example class&quot;</span></span><br><span class="line">	i=<span class="number">12345</span></span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">f</span>(<span class="params">self</span>):</span></span><br><span class="line">		<span class="keyword">return</span> <span class="string">&#x27;hello world&#x27;</span></span><br></pre></td></tr></table></figure>

<p>Myclass.i 和 Myclass.f 就是有效的属性引用，将分别返回一个整数和一个函数对象。</p>
<p>类属性也可以被赋值，因此可以通过赋值更改 MyClass.i 的值</p>
<p>_ doc _ 也是一个有效的属性，将返回所属类的文档字符串：“ A simple example class</p>
</blockquote>
</li>
<li><p>类的实例化使用函数表示法，如 x= Myclass()，创建类的新实例并将此对象分配给局部变量 x</p>
</li>
<li><p>实例化操作会创建一个空对象，许多类喜欢创建带有特定初始状态的自定义实例，为此，类定义可能包含一个名为 _ init _ ()  的特殊方法。</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">	self.data= []</span><br></pre></td></tr></table></figure>

<p>当一个类定义了 _ init _ () 方法时，类的实例化操作会自动为新创建的类实例发起调用 _ init _ () 。因此，可以通过 x=MyClass() 获得一个经初始化的新实例。</p>
</blockquote>
</li>
</ul>
</li>
<li><p>实例对象</p>
<ul>
<li>实例对象理解的唯一操作是属性引用</li>
<li>有两种有效的属性名称：数据属性和方法<ul>
<li>数据属性不需要声明，像局部变量一样，它们将在第一次被赋值时产生。</li>
<li>方法是从属于对象的函数</li>
</ul>
</li>
</ul>
</li>
<li><p>方法对象</p>
<ul>
<li><p>通常，方法在绑定后立即被调用，如 x.f()</p>
</li>
<li><p>但是，立即调用一个方法并不是必须的，x.f 是一个方法对象，它可以被保存起来以后再调用。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">xf = x.f</span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">	print(xf())</span><br></pre></td></tr></table></figure>
</li>
<li><p>方法的特殊之处就在于实例对象会作为函数的第一个参数被传入，调用 x.f() 其实就相当于 MyClass.f(x)。调用一个具有 n 个参数的方法就相当于调用再多一个参数的对应函数，这个参数值为方法所属实例对象，位置在其他参数之前。</p>
</li>
</ul>
</li>
<li><p>类和实例变量</p>
<ul>
<li><p>实例变量用于每个实例的唯一数据，类变量用于类的所有实例共享的属性和方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span>:</span></span><br><span class="line">    kind = <span class="string">&#x27;canine&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">d = Dog(<span class="string">&#x27;Fido&#x27;</span>)</span><br><span class="line">e = Dog(<span class="string">&#x27;Buddy&#x27;</span>)</span><br><span class="line">print(d.kind, d.name) <span class="comment"># canine Fido</span></span><br><span class="line">print(e.kind, e.name) <span class="comment"># canine Buddy</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>共享数据可能在设计 mutable 对象例如列表和字典的时候导致令人惊讶的结果。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span>:</span></span><br><span class="line">    tricks = []</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">add_trick</span>(<span class="params">self, trick</span>):</span></span><br><span class="line">        self.tricks.append(trick)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">c1 = Cat(<span class="string">&#x27;Fido&#x27;</span>)</span><br><span class="line">c2 = Cat(<span class="string">&#x27;Buddy&#x27;</span>)</span><br><span class="line">c1.add_trick(<span class="string">&#x27;roll over&#x27;</span>)</span><br><span class="line">c2.add_trick(<span class="string">&#x27;play dead&#x27;</span>)</span><br><span class="line">print(c1.tricks) 	<span class="comment">#[&#x27;roll over&#x27;, &#x27;play dead&#x27;]</span></span><br></pre></td></tr></table></figure>

<p>tricks 列表不应该被用作类变量，因为所有的Cat实例将只共享一个单独的列表。</p>
<p>正确的类设计应该使用实例变量</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cat</span>:</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line">        self.name = name</span><br><span class="line">        self.tricks = []  <span class="comment"># creates a new empty list for each dog</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">add_trick</span>(<span class="params">self, trick</span>):</span></span><br><span class="line">        self.tricks.append(trick)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">c1 = Cat(<span class="string">&#x27;Fido&#x27;</span>)</span><br><span class="line">c2 = Cat(<span class="string">&#x27;Buddy&#x27;</span>)</span><br><span class="line">c1.add_trick(<span class="string">&#x27;roll over&#x27;</span>)</span><br><span class="line">c2.add_trick(<span class="string">&#x27;play dead&#x27;</span>)</span><br><span class="line">print(c1.tricks)  <span class="comment"># [&#x27;roll over&#x27;]</span></span><br><span class="line">print(c2.tricks)  <span class="comment"># [&#x27;play dead&#x27;]</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>如果同样的属性名称同时出现在实例和类中，解释器会优先在当前实例对象中查找是否有该属性，如果有，则直接发返回当前实例对象的属性值，否则就去实例对象的类对象中寻找，如果有则返回类对象的属性值，类对象中依然没有，则报错。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span>:</span></span><br><span class="line">	name=<span class="string">&#x27;wangcai&#x27;</span></span><br><span class="line">	sex=<span class="string">&#x27;male&#x27;</span></span><br><span class="line"></span><br><span class="line">D1 = Dog()</span><br><span class="line">print(D1.name,D1.sex)  <span class="comment"># wangcai male</span></span><br><span class="line"></span><br><span class="line">D2 = Dog()</span><br><span class="line">D2.sex=<span class="string">&quot;female&quot;</span></span><br><span class="line">print(D2.name,D2.sex)  <span class="comment"># wangcai female</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ol>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/29/python/%E7%B1%BB%20/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/" class="post-title-link" itemprop="url">输入输出</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-28 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-28T00:00:00+08:00">2020-09-28</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:10:09" itemprop="dateModified" datetime="2020-10-04T17:10:09+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/" class="post-meta-item leancloud_visitors" data-flag-title="输入输出" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.2k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>6 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="input"><a href="#input" class="headerlink" title="input()"></a>input()</h3><ol>
<li><p>该函数用来获取用户的输入</p>
</li>
<li><p>input()调用后，程序会立即暂停，等待用户输入，用户输入完内容以后，点击回车程序才会继续向下执行</p>
</li>
<li><p>用户输入完成以后，其所输入的的内容会以返回值得形式返回</p>
</li>
<li><p>注意：input()的返回值是一个字符串</p>
</li>
<li><p>input()函数中可以设置一个字符串作为参数，这个字符串将会作为提示文字显示</p>
</li>
<li><p>input()也可以用于暂时阻止程序结束</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">username = input(<span class="string">&#x27;请输入用户名:&#x27;</span>)</span><br><span class="line"><span class="keyword">if</span> username == <span class="string">&#x27;admin&#x27;</span>:</span><br><span class="line">    print(<span class="string">&#x27;管理员登录！&#x27;</span>)</span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ol>
<h3 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h3><ol>
<li><p>几种格式化输出的方法</p>
<ul>
<li><p>格式化字符串</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 字符串相加，拼接（注意，字符串只能和字符串拼接，不能和其他类型做加法连接）</span></span><br><span class="line">a = <span class="number">123</span> </span><br><span class="line">print(<span class="string">&quot;a = &quot;</span>+a) <span class="comment"># 不常用</span></span><br><span class="line">print(<span class="string">&#x27;a =&#x27;</span>,a)  <span class="comment"># 常用</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># a = 123</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建字符串时，可以在字符串中指定占位符</span></span><br><span class="line"><span class="comment"># %s 在字符串中表示任意字符</span></span><br><span class="line"><span class="comment"># %3s，3位字符，不够补空格</span></span><br><span class="line"><span class="comment"># %3.5s，3位到5位字符</span></span><br><span class="line"><span class="comment"># %f 浮点数占位符</span></span><br><span class="line"><span class="comment"># %.2f 两位小数</span></span><br><span class="line"><span class="comment"># %d 整数占位符</span></span><br><span class="line"></span><br><span class="line">a = <span class="string">&#x27;Hello %s&#x27;</span> % <span class="string">&#x27;张三&#x27;</span>  <span class="comment"># a = Hello 张三</span></span><br><span class="line">b = <span class="string">&#x27;hello %s 你好 %s&#x27;</span> % (<span class="string">&#x27;Tom&#x27;</span>, <span class="string">&#x27;张三&#x27;</span>)  <span class="comment"># b = hello Tom 你好 张三</span></span><br><span class="line">c = <span class="string">&#x27;hello %3.5s&#x27;</span> % <span class="string">&#x27;abcdefg&#x27;</span>  <span class="comment"># c = hello abcde</span></span><br><span class="line">d = <span class="string">&#x27;hello %s&#x27;</span> % <span class="number">123.456</span>  <span class="comment"># d = hello 123.456</span></span><br><span class="line">e = <span class="string">&#x27;hello %.2f&#x27;</span> % <span class="number">123.456</span>  <span class="comment"># e = hello 123.46</span></span><br><span class="line">f = <span class="string">&#x27;hello %d&#x27;</span> % <span class="number">123.95</span>  <span class="comment"># f = hello 123</span></span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;a = %s&#x27;</span> % a)</span><br><span class="line">print(<span class="string">&#x27;b = %s&#x27;</span> % b)</span><br><span class="line">print(<span class="string">&#x27;c = %s&#x27;</span> % c)</span><br><span class="line">print(<span class="string">&#x27;d = %s&#x27;</span> % d)</span><br><span class="line">print(<span class="string">&#x27;e = %s&#x27;</span> % e)</span><br><span class="line">print(<span class="string">&#x27;f = %s&#x27;</span> % f)</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在字符串的引号之前加上一个 f 或 F，在字符串中，可以在&#123;&#125; 之间写可以引用的变量或字面值的 python 表达式</span></span><br><span class="line">year = <span class="number">2016</span></span><br><span class="line">str = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line">print(&#123;year&#125;, &#123;str&#125;)</span><br><span class="line">print(<span class="string">f&#x27;year=<span class="subst">&#123;year&#125;</span>,str=<span class="subst">&#123;str&#125;</span>&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">&#123;2016&#125; &#123;&#x27;hello world&#x27;&#125;</span></span><br><span class="line"><span class="string">year = 2016, str = hello world</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

</blockquote>
</li>
<li><p>字符串的 str.format() 方法</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">42</span>_572_654</span><br><span class="line">b = <span class="number">43</span>_132_495</span><br><span class="line">c = a / (a + b)</span><br><span class="line">print(<span class="string">&#x27;&#123;:-9&#125;,&#123;:2.2%&#125;&#x27;</span>.format(a, c))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">42572654,49.67%</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

<p>仍然使用{} 来标记变量将被替换的位置，并且可以提供详细的格式化指令，但还需要提供要格式化的信息</p>
</blockquote>
</li>
<li><p>当只想快速显示某些变量以进行调试时，可以使用 repr() 或 str() 将任何值转化为字符串</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">s = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line">print(str(s), repr(s))  <span class="comment"># hello world  &#x27;hello world&#x27;</span></span><br><span class="line">print(str(<span class="number">1</span> / <span class="number">7</span>))  <span class="comment"># 0.14285714285714285</span></span><br><span class="line"></span><br><span class="line">x = <span class="number">10</span> * <span class="number">3.25</span></span><br><span class="line">y = <span class="number">200</span> * <span class="number">200</span></span><br><span class="line">print(repr(x), repr(y))  <span class="comment"># 32.5 40000</span></span><br><span class="line">print(repr((x, y, (<span class="string">&#x27;Tom&#x27;</span>, <span class="string">&#x27;Bob&#x27;</span>))))  <span class="comment"># (32.5, 40000, (&#x27;Tom&#x27;, &#x27;Bob&#x27;))</span></span><br><span class="line"></span><br><span class="line">str = <span class="string">&quot;hello,world\n&quot;</span></span><br><span class="line">print(repr(str))  <span class="comment"># &#x27;hello,world\n&#x27;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
<li><p>格式化字符串文字</p>
<ul>
<li><p>格式化字符串字面值（f-字符串），可以在字符串前面加上 f 和 F 并将表达式写成 {expression} ，在字符串中包含python表达式的值</p>
</li>
<li><p>可选的格式说明符可以跟在表达式后，可以更好的控制值的格式化方式</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 将PI舍入到小数点后三位</span></span><br><span class="line"><span class="keyword">import</span> math</span><br><span class="line"></span><br><span class="line">print(<span class="string">f&#x27;PI is <span class="subst">&#123;math.pi:<span class="number">.3</span>f&#125;</span>&#x27;</span>)  <span class="comment"># PI is 3.142</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>在 ’ ：’ 后传递一个整数可以让该字段成为最小字符宽度，在列对齐时很有用</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">table = &#123;<span class="string">&#x27;Tom&#x27;</span>: <span class="number">4127</span>, <span class="string">&#x27;Jack&#x27;</span>: <span class="number">4098</span>, <span class="string">&#x27;Alice&#x27;</span>: <span class="number">7678</span>&#125;</span><br><span class="line"><span class="keyword">for</span> name, phone <span class="keyword">in</span> table.items():</span><br><span class="line">    print(<span class="string">f&#x27;<span class="subst">&#123;name:<span class="number">10</span>&#125;</span>==&gt;<span class="subst">&#123;phone:<span class="number">10</span>d&#125;</span>&#x27;</span>)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">Tom       ==&gt;      4127</span></span><br><span class="line"><span class="string">Jack      ==&gt;      4098</span></span><br><span class="line"><span class="string">Alice     ==&gt;      7678</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>其他修饰符可用于在格式化前转化值。’ !a ‘ 应用 ascill() ，’ !s ‘ 应用 str()，’ !r ‘ 应用 repr()</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">animals = <span class="string">&#x27;eels&#x27;</span></span><br><span class="line">print(<span class="string">f&#x27;<span class="subst">&#123;animals&#125;</span>&#x27;</span>)  <span class="comment"># eels</span></span><br><span class="line">print(<span class="string">f&#x27;<span class="subst">&#123;animals!a&#125;</span>&#x27;</span>)  <span class="comment"># &#x27;eels&#x27;</span></span><br><span class="line">print(<span class="string">f&#x27;<span class="subst">&#123;animals!s&#125;</span>&#x27;</span>)  <span class="comment"># eels</span></span><br><span class="line">print(<span class="string">f&#x27;<span class="subst">&#123;animals!r&#125;</span>&#x27;</span>)  <span class="comment"># &#x27;eels&#x27;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
<li><p>字符串的 format() 方法</p>
<ul>
<li><p>基本用法</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;&#123;&#125;,&quot;&#123;&#125;&quot;&#x27;</span>.format(<span class="string">&#x27;hello&#x27;</span>, <span class="string">&#x27;world&#x27;</span>))  <span class="comment"># hello,&quot;world&quot;</span></span><br></pre></td></tr></table></figure>

</blockquote>
<p>花括号和其中的字符（称为格式字段）将替换为传递给 str.format() 方法的对象。</p>
</li>
<li><p>花括号中的数字可以表示传递给 str.format() 方法的对象的位置</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;&#123;0&#125; and &#123;1&#125;&#x27;</span>.format(<span class="string">&#x27;abc&#x27;</span>, <span class="string">&#x27;ABC&#x27;</span>))  <span class="comment"># abc and ABC</span></span><br><span class="line">print(<span class="string">&#x27;&#123;1&#125; and &#123;0&#125;&#x27;</span>.format(<span class="string">&#x27;abc&#x27;</span>, <span class="string">&#x27;ABC&#x27;</span>))  <span class="comment"># ABC and abc</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>如果在 str.format() 方法中使用关键字参数，则使用参数的名称引用它们的值</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;&#123;name&#125; and &#123;sex&#125;&#x27;</span>.format(name=<span class="string">&#x27;jack&#x27;</span>, sex=<span class="string">&#x27;男&#x27;</span>))  <span class="comment"># jack and 男</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>位置和关键字参数可以任意组合</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;&#123;0&#125;,&#123;1&#125;,and &#123;other&#125;&#x27;</span>.format(<span class="string">&#x27;abc&#x27;</span>, <span class="string">&#x27;ABC&#x27;</span>, other=<span class="string">&#x27;hello&#x27;</span>))  <span class="comment"># abc,ABC,and hello</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>如果是一个很长的格式字符串，不希望把它拆开，那么最好按名称而不是按位置引用变量来进行格式化，这可以通过简单的传递字典并使用方括号访问键来完成</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">table = &#123;<span class="string">&#x27;Tom&#x27;</span>: <span class="number">4127</span>, <span class="string">&#x27;Jack&#x27;</span>: <span class="number">4098</span>, <span class="string">&#x27;Alice&#x27;</span>: <span class="number">7678</span>&#125;</span><br><span class="line">print(<span class="string">&#x27;Tom:&#123;0[Tom]:d&#125;;Jack:&#123;0[Jack]:d&#125;;Alice:&#123;0[Alice]:d&#125;;&#x27;</span>.format(table)) <span class="comment">#Tom:4127;Jack:4098;Alice:7678;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>也可以通过使用 ‘*’ 符号将 table 作为关键字参数传递</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;Tom:&#123;Tom:d&#125;;Jack:&#123;Jack:d&#125;;Alice:&#123;Alice:d&#125;;&#x27;</span>.format(**table)) <span class="comment">#Tom:4127;Jack:4098;Alice:7678;</span></span><br></pre></td></tr></table></figure>

</blockquote>
</li>
</ul>
<p>这与内置函数 vars() 结合使用时非常有用，会返回包含所有局部变量的字典</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 下面会生成一组整齐的列，其中包含给定的整数和它的平方以及立方</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>):</span><br><span class="line">    print(<span class="string">&#x27;&#123;0:2d&#125; &#123;1:3d&#125; &#123;2:4d&#125;&#x27;</span>.format(x,x*x,x*x*x))</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string"> 1   1    1</span></span><br><span class="line"><span class="string"> 2   4    8</span></span><br><span class="line"><span class="string"> 3   9   27</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>手动格式化字符串</p>
<ul>
<li><p>平方和立方的表，手动格式化的，每一列之间的一个空格是通过 print() 方式添加的</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>):</span><br><span class="line">    print(repr(x).rjust(<span class="number">2</span>),repr(x*x).rjust(<span class="number">3</span>),end=<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">    print(repr(x*x*x).rjust(<span class="number">4</span>))</span><br><span class="line">    </span><br><span class="line">   </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string"> 1   1   1</span></span><br><span class="line"><span class="string"> 2   4   8</span></span><br><span class="line"><span class="string"> 3   9  27</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

</blockquote>
<p>字符串的 str.rjust() 方法通过在左侧填充空格来对给定宽度的字段中的字符串进行右对齐。。类似的还有 str.ljust()  和 str.center() 。</p>
</li>
<li><p>str.zfill() ，它会在数字字符串的左边填充0，能识别正负号。</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="string">&#x27;123&#x27;</span></span><br><span class="line">b = <span class="string">&#x27;-3.14&#x27;</span></span><br><span class="line">print(a.zfill(<span class="number">5</span>))  <span class="comment"># 00123</span></span><br><span class="line">print(b.zfill(<span class="number">7</span>))  <span class="comment"># -003.14</span></span><br><span class="line">print(b.zfill(<span class="number">1</span>))  <span class="comment"># -3.14</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
<li><p>旧的字符串格式化方法</p>
<ul>
<li><p>% 运算符（求余）也可用于字符串格式化，给定 ‘ string ‘  % values，则 string 中的 % 实例会以0个或多个 values 元素替换。此操作通常被称为字符串插值</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> math</span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;PI is %5.3f&#x27;</span> % math.pi)  <span class="comment"># PI is 3.142</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
</ol>
<h3 id="读写文件"><a href="#读写文件" class="headerlink" title="读写文件"></a>读写文件</h3>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/27/python/%E6%A8%A1%E5%9D%97/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/27/python/%E6%A8%A1%E5%9D%97/" class="post-title-link" itemprop="url">模块</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-27 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-27T00:00:00+08:00">2020-09-27</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:03:26" itemprop="dateModified" datetime="2020-10-04T17:03:26+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/27/python/%E6%A8%A1%E5%9D%97/" class="post-meta-item leancloud_visitors" data-flag-title="模块" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/27/python/%E6%A8%A1%E5%9D%97/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/27/python/%E6%A8%A1%E5%9D%97/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>4 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="模块"><a href="#模块" class="headerlink" title="模块"></a>模块</h3><ol>
<li><p>如果从python 解释器退出并再次进入，那么之前定义的函数和变量都会丢失，因此，如果想编写一个稍微长一些的程序，最好使用功能文本编辑器（如pycharm) 为解释器准备输入并将该文件作为输入运行，这被称为编写 脚本</p>
</li>
<li><p>为了将程序拆分为几个文件以方便维护，并且希望在不同程序中使用同一个函数而不是把和这个函数复制到每一个程序中去，可以使用 python提供的模块。</p>
</li>
<li><p>模块可以把定义放在一个文件中，并在脚本或解释器的交互式实例中使用它们， 模块中的定义可以导入到其他模块或者主模块（顶级和计算器模式下执行的脚本中可以访问的变量集合）中</p>
</li>
<li><p>在一个模块中引入外部模块</p>
<ul>
<li>import  模块名 </li>
<li>import  模块名   as  模块别名</li>
<li>也可以只引入模块中的部分内容，from 模块名 import  …….</li>
<li>可以引入同一个模块多次，但是模块的实例只会创建一次</li>
<li>import  可以在程序的任意位置调用，一般写在程序开头</li>
<li>在一个模块内部，模块名可以通过全局变量” _ name _”的值获得，__  name __ 属性值为 __ main __ 的模块是主模块，一个程序中只会有一个主模块</li>
</ul>
</li>
<li><p>例如：</p>
<blockquote>
<p>a.py</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">sum</span>(<span class="params">a, b</span>):</span></span><br><span class="line">    <span class="keyword">return</span> a + b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">cut</span>(<span class="params">a, b</span>):</span></span><br><span class="line">    <span class="keyword">return</span> a - b</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>然后在该目录中进入python解释器，导入该模块</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>imnport a</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>在当前的符号表中，这并不会直接进入到定义在 a 函数内的名称，只是进入到该模块名中  a 中，可以用模块名访问 sum 和 cut 函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.sum(<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.cut(<span class="number">10</span>,<span class="number">4</span>)</span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.__name__</span><br><span class="line"><span class="string">&#x27;a&#x27;</span></span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>模块可以包含可执行的语句以及函数定义，这些语句用于初始化模块，它们仅在模块第一次 import  时才执行</p>
</li>
<li><p>每个模块都有它自己的私有符号表，该表用作模块中定义的所有函数的全局符号表。因此，模块的作者可以在模块内使用全局变量而不必担心与用户的全局变量发生冲突。</p>
</li>
<li><p>模块可以导入其他模块，习惯上把所有 import 语句放在模块或脚本的开头，但不要求。被导入的模块名存放在调入模块的全局符号表中</p>
</li>
<li><p>import 语句有一个变体，可以把名字从一个被调模块内直接导入到现模块的符号表里，</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> a <span class="keyword">import</span> sum,cut</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>sum(<span class="number">1</span>,<span class="number">1</span>)</span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>cut(<span class="number">3</span>,<span class="number">4</span>)</span><br><span class="line"><span class="number">-1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.__name__</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">NameError: name <span class="string">&#x27;a&#x27;</span> <span class="keyword">is</span> <span class="keyword">not</span> defined</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure>

<p>这并不会把被调入模块名引入到局部变量里，即这么做 a 是未定义的</p>
</blockquote>
</li>
<li><p>还有一个变体甚至可以导入模块内定义的所有名称</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> a  <span class="keyword">import</span> *</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>sum(<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.__name__</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">NameError: name <span class="string">&#x27;a&#x27;</span> <span class="keyword">is</span> <span class="keyword">not</span> defined</span><br></pre></td></tr></table></figure>

<p>这会调入所有不是以下划线(_)开头的名称，在多数情况下，这个功能并不常用，因为会导致代码可读性很差。</p>
</blockquote>
</li>
<li><p>如果模块名称之后带有 as ，在跟在 as 之后的名称将直接绑定到所导入的模块</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> a <span class="keyword">as</span> module_a</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>module_a.sum(<span class="number">1</span>,<span class="number">3</span>)</span><br><span class="line"><span class="number">4</span></span><br></pre></td></tr></table></figure>

<p>这种方式也可以在用到 from 的时候使用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> a <span class="keyword">import</span> sum <span class="keyword">as</span> function_sum</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>function_sum(<span class="number">3</span>,<span class="number">4</span>)</span><br><span class="line"><span class="number">7</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ol>
<ol start="12">
<li><p>以脚本的方式执行模块</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>python a.py &lt;arguments&gt;</span><br></pre></td></tr></table></figure>

<p>模块里的代码会被执行，就像导入了模块一样，但是__ name __ 被赋值为“ __ main __”</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>__name__</span><br><span class="line"><span class="string">&#x27;__main__&#x27;</span></span><br></pre></td></tr></table></figure>

<p>这经常用于为模块提供一个方便的用户接口，或用于测试（以脚本的方式运行模块从而执行一些测试套件）</p>
</blockquote>
</li>
<li><p>模块搜索路径</p>
<blockquote>
<p>当一个名为 spam 的模块被导入的时候，解释器首先寻找具有该名称的内置模块。</p>
<p>如果没找到，然后解释器从 sys.path 变量给出的目录列表中寻找名为 spam.py的文件，sys.path 初始有以下目录地址:</p>
<p>​        包含输入脚本的目录，或者未指定文件时的当前目录</p>
<p>​        PYTHONPATH—–一个包含目录名称的列表，它和 shell 变量 PAHT有一样的语法</p>
<p>​        取决于安装的默认的配置</p>
<p>在初始化后，Python程序可以更改 sys.path，包含正在运行脚本的文件目录被放在搜索路径的开头处，标准库路径之前。这意味着将加载此目录里的脚本，而不是标准库中的同名模块。</p>
</blockquote>
</li>
<li><p>“编译过的” python 文件</p>
<blockquote>
<p>为了加速模块载入，Python在 <em>pycache</em> 目录里缓存了每个模块的编译后版本，名称为 moudle.version.pyc，其中名称中的版本字段对编译文件的格式进行编码，一般使用Python版本号。</p>
<p>例如，在CPython3.3中，spam.py的编译版本将被缓存为 _ pycache _/spam.cpython-33.pyc</p>
<p>python在两种情况下不会检查缓存，一是从命令行直接载入的模块，它都是重新编译不存储编译结果；其次，如果没有源模块，它不会检查缓存。</p>
</blockquote>
</li>
</ol>
<h3 id="标准模块"><a href="#标准模块" class="headerlink" title="标准模块"></a>标准模块</h3><ol>
<li><p>python附带了一个标准模块库</p>
</li>
<li><p>一些模块内置于解释器中，它们提供对不属于语言核心但仍然内置的操作的访问，以提高效率或提供对系统调用等操作系统原语的访问</p>
</li>
<li><p>这些模块的集合是一个配置选项，也取决于底层平台，例如，winreg 模块只在 windows 操作系统上提供</p>
</li>
<li><p>特别注意 sys 模块，它被内嵌到每一个Python解释器中，使我们可以获取到python解释器的信息或者通过函数来操作python解释器，变量 sys.psl 和 sys.ps2 定义用作主要和辅助提示的字符串</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> sys</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>sys.ps1</span><br><span class="line"><span class="string">&#x27;&gt;&gt;&gt; &#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>sys.ps2</span><br><span class="line"><span class="string">&#x27;... &#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>sys.ps1=<span class="string">&#x27;C&gt;&#x27;</span></span><br><span class="line">C&gt;print(<span class="string">&#x27;Hello&#x27;</span>)</span><br><span class="line">Hello</span><br><span class="line">C&gt;</span><br></pre></td></tr></table></figure>

<p>这两个变量只有在编译器是交互模式下才被定义</p>
</blockquote>
</li>
<li><p>sys.argv，获取执行代码时，命令行中所包含的参数，返回一个列表，列表中保存了当前命令的所有参数</p>
</li>
<li><p>sys.modules，获取当前程序中引入的所有模块，返回一个字典，字典的 key 是模块的名字，字典的 value 是模块对象</p>
</li>
<li><p>syp.path 变量是一个字符串列表，用于确定解释器的模块搜索路径，该变量被初始化为从环境变量 PYTHONPATH获取的默认路径，或者如果PYTHONPATH未设置，则从内置默认路径初始化。</p>
</li>
<li><p>sys.platform，表示当前python运行的平台，windows平台下，值为 win32</p>
</li>
<li><p>sys.exit() 用来退出程序，参数中可以添加退出提示</p>
</li>
<li><p>pprint 模块给我们提供了一个方法 pprint() ，该方法可以用来对打印的数据做简单的格式化</p>
</li>
<li><p>os 模块可以对操作系统进行访问，os.environ，通过这个属性可以获取到系统的环境变量；os.system，可以用来执行操作系统的名字；</p>
</li>
</ol>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/27/python/%E6%A8%A1%E5%9D%97/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" class="post-title-link" itemprop="url">数据结构</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-27 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-27T00:00:00+08:00">2020-09-27</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:09:48" itemprop="dateModified" datetime="2020-10-04T17:09:48+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" class="post-meta-item leancloud_visitors" data-flag-title="数据结构" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>24k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>22 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h3><ol>
<li><p>列表是python中的一个对象</p>
<ol start="2">
<li><p>数值只能保存一个单一的数据，列表中可以保存多个有序的数据</p>
</li>
<li><p>列表的创建：通过[] 创建</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = []  <span class="comment"># 创建了一个空列表</span></span><br><span class="line">print(list, type(list))  <span class="comment"># [] &lt;class &#x27;list&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ol>
</li>
<li><p>一个列表中可以存储多个元素，也可以在创建列表时，指定列表中的元素</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">5</span>]  <span class="comment"># 创建一个包含5个元素的列表</span></span><br><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]  <span class="comment"># 创建了一个有5个元素的列表</span></span><br><span class="line">print(list)  <span class="comment"># [10, 20, 30, 40, 50]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>列表中可以保存任意的对象</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="string">&#x27;hello&#x27;</span>, <span class="literal">True</span>, <span class="literal">None</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="keyword">print</span>]</span><br><span class="line">print(list)  <span class="comment"># [10, &#x27;hello&#x27;, True, None, [1, 2, 3], &lt;built-in function print&gt;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>可以通过索引获取列表中的元素</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">print(list[<span class="number">4</span>])  <span class="comment"># 50</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>获取列表的长度，len()</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">print(len(list))  <span class="comment"># 5</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>+，将两个列表拼接为一个列表</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] + [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">print(list)  <span class="comment"># [1, 2, 3, 4, 5, 6]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>’ * ‘，将列表重复指定次数</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] * <span class="number">2</span></span><br><span class="line">print(list)  <span class="comment"># [1, 2, 3, 1, 2, 3]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>in 和 not in ，检查指定元素是否存在于列表中，返回True或false</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line"></span><br><span class="line">print(<span class="number">10</span> <span class="keyword">not</span> <span class="keyword">in</span> list)  <span class="comment"># False</span></span><br><span class="line">print(<span class="number">10</span> <span class="keyword">in</span> list)  <span class="comment"># True</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>min() 获取列表中最小值， max() 获取列表中最大值</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>]</span><br><span class="line">print(min(list), max(list))  <span class="comment"># 10 50</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>列表对象方法</p>
</li>
</ol>
<blockquote>
<p>list.append(x)：在列表的末尾添加一个元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fruits = [<span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>, <span class="string">&#x27;kiwi&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">fruits.append(<span class="string">&#x27;grape&#x27;</span>) </span><br><span class="line">print(fruits) <span class="comment">#[&#x27;orange&#x27;, &#x27;apple&#x27;, &#x27;pear&#x27;, &#x27;banana&#x27;, &#x27;kiwi&#x27;, &#x27;apple&#x27;, &#x27;banana&#x27;, &#x27;grape&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.extend(iterable)：使用可迭代对象中的所有元素来扩展列表，相当于+=</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>]</span><br><span class="line">list.extend([<span class="string">&#x27;刘晨&#x27;</span>,<span class="string">&#x27;王立&#x27;</span>]) </span><br><span class="line"><span class="comment">#[&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;, &#x27;刘晨&#x27;, &#x27;王立&#x27;]</span></span><br><span class="line"></span><br><span class="line">list += [<span class="string">&#x27;王敏&#x27;</span>,<span class="string">&#x27;朱丽&#x27;</span>] </span><br><span class="line"><span class="comment">#[&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;, &#x27;刘晨&#x27;, &#x27;王立&#x27;, &#x27;王敏&#x27;, &#x27;朱丽&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>list.inert(i，x)：在给定的位置插入一个元素，第一个参数是要插入的元素的索引，第二个参数是要插入的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;李华&#x27;</span>]</span><br><span class="line">list.insert(<span class="number">2</span>, <span class="string">&#x27;Jack&#x27;</span>)</span><br><span class="line">print(list)  <span class="comment"># [&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;Jack&#x27;, &#x27;王五&#x27;, &#x27;李华&#x27;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/27/python/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/26/python/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/26/python/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/" class="post-title-link" itemprop="url">流程控制语句</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-26 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-26T00:00:00+08:00">2020-09-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:06:44" itemprop="dateModified" datetime="2020-10-04T17:06:44+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/26/python/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/" class="post-meta-item leancloud_visitors" data-flag-title="流程控制语句" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/26/python/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/26/python/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>3.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>3 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="if语句"><a href="#if语句" class="headerlink" title="if语句"></a>if语句</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">age = <span class="number">68</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> age &gt;= <span class="number">18</span> <span class="keyword">and</span> age &lt; <span class="number">30</span>:</span><br><span class="line">    print(<span class="string">&#x27;已成年&#x27;</span>)</span><br><span class="line"><span class="keyword">elif</span> age &gt;= <span class="number">30</span> <span class="keyword">and</span> age &lt; <span class="number">60</span>:</span><br><span class="line">    print(<span class="string">&#x27;中年&#x27;</span>)</span><br><span class="line"><span class="keyword">elif</span> age &gt;= <span class="number">60</span>:</span><br><span class="line">    print(<span class="string">&#x27;已退休&#x27;</span>)</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">x = <span class="number">10</span></span><br><span class="line"><span class="keyword">if</span> x &lt; <span class="number">0</span>:</span><br><span class="line">    x = <span class="number">0</span></span><br><span class="line"><span class="keyword">elif</span> (x % <span class="number">2</span>) == <span class="number">0</span>:</span><br><span class="line">    print(<span class="string">&quot;输入的是偶数&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    print(<span class="string">&quot;输入的是奇数&quot;</span>)</span><br></pre></td></tr></table></figure>

<ul>
<li>可以有0个或多个 elif 部分，以及一个可选的 else 部分，关键字 ‘elif’ 是 ‘else if ‘ 的缩写。</li>
<li>一个if…elif..elif…序列可以看做其他语言中的 swtich 或 case语句</li>
</ul>
<h3 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h3>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/26/python/%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/26/python/%E7%AC%AC%E4%B8%80%E4%B8%AApython%E7%A8%8B%E5%BA%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/26/python/%E7%AC%AC%E4%B8%80%E4%B8%AApython%E7%A8%8B%E5%BA%8F/" class="post-title-link" itemprop="url">关于python</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-26 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-26T00:00:00+08:00">2020-09-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:15:59" itemprop="dateModified" datetime="2020-10-04T17:15:59+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/26/python/%E7%AC%AC%E4%B8%80%E4%B8%AApython%E7%A8%8B%E5%BA%8F/" class="post-meta-item leancloud_visitors" data-flag-title="关于python" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/26/python/%E7%AC%AC%E4%B8%80%E4%B8%AApython%E7%A8%8B%E5%BA%8F/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/26/python/%E7%AC%AC%E4%B8%80%E4%B8%AApython%E7%A8%8B%E5%BA%8F/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>3 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="python"><a href="#python" class="headerlink" title="python"></a>python</h3><ol>
<li>python提供了高效的高级数据结构，有高级的内置数据类型，比如灵活的数组和字典，能简单有效地面向对象编程</li>
<li>python是解释型语言</li>
<li>编译型语言和解释型语言<ul>
<li>任何语言给计算机执行时，都要转换成机器码 （二进制的0101）</li>
<li>根据转换时机不同分为两类<ul>
<li>编译型：如C语言，在代码执行前，编译为机器码，执行速度快，跨平台性不好</li>
<li>解释型：python，JS，java，不会在执行前对代码编译，而是边执行边编译，由解释器解释执行，执行速度慢，跨平台性好</li>
</ul>
</li>
</ul>
</li>
<li>python缺点：<ul>
<li>运行速度慢，python是解释性语言，代码在执行时会一行一行地翻译成CPU能理解的机器码，这个翻译过程非常耗时，所以很慢。C程序是运行前直接编译成CPU能执行的机器码，所以块。</li>
<li>代码不能加密，发布python程序实际上就是发布源代码。C语言不用发布源代码，只需要把编译后的机器码（xxx.exe文件) 发布出去，要从机器码反推出C代码是不可能的，所以，凡是编译型的语言，都没有这个问题，而解释型的语言，则必须把源代码发布出去。</li>
</ul>
</li>
<li>Python标准库：<a target="_blank" rel="noopener" href="https://docs.python.org/zh-cn/3/library/index.html">https://docs.python.org/zh-cn/3/library/index.html</a></li>
<li>Python语法：<a target="_blank" rel="noopener" href="https://docs.python.org/zh-cn/3/reference/index.html">https://docs.python.org/zh-cn/3/reference/index.html</a></li>
</ol>
<h3 id="python解释器"><a href="#python解释器" class="headerlink" title="python解释器"></a>python解释器</h3><ul>
<li><p>目前，python有两个版本，2.x版和3.x版，这两个版本不兼容。</p>
</li>
<li><p>python3安装：<a target="_blank" rel="noopener" href="https://www.python.org/downloads/windows/">https://www.python.org/downloads/windows/</a></p>
</li>
<li><p>命令行选项：<a target="_blank" rel="noopener" href="https://docs.python.org/zh-cn/3/using/cmdline.html#using-on-general">https://docs.python.org/zh-cn/3/using/cmdline.html#using-on-general</a></p>
</li>
</ul>
<h3 id="python作为计算器使用"><a href="#python作为计算器使用" class="headerlink" title="python作为计算器使用"></a>python作为计算器使用</h3>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/26/python/%E7%AC%AC%E4%B8%80%E4%B8%AApython%E7%A8%8B%E5%BA%8F/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  

      
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/26/%E5%AF%86%E7%A0%81%E5%AD%A6/%E5%AF%86%E7%A0%81%E5%AD%A6%E6%A6%82%E8%BF%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/26/%E5%AF%86%E7%A0%81%E5%AD%A6/%E5%AF%86%E7%A0%81%E5%AD%A6%E6%A6%82%E8%BF%B0/" class="post-title-link" itemprop="url">密码学概述</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-26 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-26T00:00:00+08:00">2020-09-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:07:18" itemprop="dateModified" datetime="2020-10-04T17:07:18+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%AF%86%E7%A0%81%E5%AD%A6/" itemprop="url" rel="index"><span itemprop="name">密码学</span></a>
        </span>
    </span>

  
    <span id="/2020/09/26/%E5%AF%86%E7%A0%81%E5%AD%A6/%E5%AF%86%E7%A0%81%E5%AD%A6%E6%A6%82%E8%BF%B0/" class="post-meta-item leancloud_visitors" data-flag-title="密码学概述" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/26/%E5%AF%86%E7%A0%81%E5%AD%A6/%E5%AF%86%E7%A0%81%E5%AD%A6%E6%A6%82%E8%BF%B0/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/26/%E5%AF%86%E7%A0%81%E5%AD%A6/%E5%AF%86%E7%A0%81%E5%AD%A6%E6%A6%82%E8%BF%B0/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>3 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="密码学主要解决的问题"><a href="#密码学主要解决的问题" class="headerlink" title="密码学主要解决的问题"></a>密码学主要解决的问题</h3><ul>
<li>信息的机密性、完整性、不可否认性和可用性</li>
<li>机密性：对传送的信息进行加密就可以实现机密性，保证信息不泄露给未经授权的人。</li>
<li>完整性：防止信息被未经授权的人篡改，保证信息不被篡改</li>
<li>不可否认性：保证信息行为人不能否认其信息行为</li>
<li>可用性：保证信息及信息系统确实为授权使用者所用</li>
</ul>
<h3 id="密码学起源与发展"><a href="#密码学起源与发展" class="headerlink" title="密码学起源与发展"></a>密码学起源与发展</h3><ol>
<li>古典密码学<ol>
<li>典型的古典密码算法包括凯撒（Caeser) 密码和维基利亚（Vigenere）密码</li>
<li>主要特点：数据的安全基于算法的保密</li>
</ol>
</li>
<li>近代密码学<ol>
<li>主要特点：数据的安全基于密钥而不是算法的保密</li>
</ol>
</li>
<li>现代密码学<ol>
<li>RSA公钥密码算法</li>
<li>椭圆曲线等其他公钥算法</li>
</ol>
</li>
</ol>
<h3 id="密码学概念"><a href="#密码学概念" class="headerlink" title="密码学概念"></a>密码学概念</h3>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2020/09/26/%E5%AF%86%E7%A0%81%E5%AD%A6/%E5%AF%86%E7%A0%81%E5%AD%A6%E6%A6%82%E8%BF%B0/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
  
  
  


  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/9/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/9/">9</a><span class="page-number current">10</span><a class="page-number" href="/page/11/">11</a><span class="space">&hellip;</span><a class="page-number" href="/page/13/">13</a><a class="extend next" rel="next" href="/page/11/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shanlu</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">225k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:25</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>


    <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="//cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  <script src="/js/local-search.js"></script>















  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  
<script>
NexT.utils.loadComments('#valine-comments', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', () => {
    new Valine(Object.assign({
      el  : '#valine-comments',
      path: "/page/10/",
    }, {"enable":true,"appId":"M0TTE46ti90I3BUUJqD5WYyD-gzGzoHsz","appKey":"UelegRsJXosPYDakAbUdO5e8","notify":true,"verify":false,"placeholder":"留下评论吧","avatar":"mm","meta":["nick","mail","link"],"pageSize":10,"lang":"zh-cn","visitor":true,"comment_count":true,"recordIP":false,"serverURLs":null,"enableQQ":false,"requiredFields":[]}
    ));
  }, window.Valine);
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":225,"height":450},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body>
</html>
